﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Net;



public partial class admin_csIPNexample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Post back to either sandbox or live
        string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
        string strLive = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);

        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);
        strRequest += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;
       
        
        if (Request != null)
        {
            string itemname = "";
            string itemnumber = "";
            string gross = "";
            string client = "";
            

            Utility.writeError("Paypal Request:" + strRequest);
            int items = 1;
            if (Request.Params.Get("num_cart_items") != null && Request.Params.Get("num_cart_items") != "")
            {
                items= int.Parse(Request.Params.Get("num_cart_items")); // numero de items que contiene
            }
           //  Utility.writeError("Paypal numero de items:"+items);

                for (int i = 0; i < items; i++)
                {
                    int a = i + 1;
                    string itemnumbers = "item_number"+a;
                    //Utility.writeError("Paypal buscando item_number:"+itemnumbers);
                    if (!itemnumber.Equals("")) itemnumber += ";";
                    itemnumber += Request.Params.Get(itemnumbers);

                    string mcgross = "mc_gross_" + a;
                   // Utility.writeError("Paypal buscando gross:" + mcgross);
                    if (!gross.Equals("")) gross += ";";
                    gross += Request.Params.Get(mcgross);

                    string itemnames = "item_name" + a;
                   // Utility.writeError("Paypal buscando itemname:" + itemnames);
                    if (!client.Equals("")) client += ";";
                    client += Request.Params.Get(itemnames);

                }
            
          
            string grossgeneral = Request.Params.Get("mc_gross"); //monto generaql 
            string payment_status = Request.Params.Get("payment_status");
            string tranid = Request.Params.Get("txn_id");
            string ipnid = Request.Params.Get("ipn_track_id");
            string tx_type = Request.Params.Get("txn_type");
        

         /*  string grossgeneral = "6.50"; //monto generaql 
            string payment_status = "Completed";
            string tranid = "89K942384A7657357";
            string ipnid = "532ed8f1b58e7";
            string tx_type = "cart";
            gross = "0.50;6.00";
            itemnumber = "21762;21767;21768";
            client = "JOSH JANSEN;JOSH JANSEN";
          * 
          * */
            string[] tempclient = client.Split(';');
            RE_GenericBean datos = new RE_GenericBean();
            datos.strC1 = tx_type;
            datos.strC2 = grossgeneral;//payment_fee
            datos.strC20 = gross;
            datos.strC3 = "";//subject
            datos.strC4 = tranid;//ipn id
            datos.strC5 = payment_status;//paymentstatus
            datos.strC6 = itemnumber;//items
            datos.strC7 = tempclient[0]; //cliente

          


            int resp = DB.setPaidPaypayAutomatic(datos);

            StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();
            StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            string strResponse = streamIn.ReadToEnd();
            streamIn.Close();

            if (strResponse == "VERIFIED")
            {
                //check the payment_status is Completed
                //check that txn_id has not been previously processed
                //check that receiver_email is your Primary PayPal email
                //check that payment_amount/payment_currency are correct
                //process payment
                Utility.writeError("Paypal VERIFIED");

            }
            else if (strResponse == "INVALID")
            {
                Utility.writeError("Paypal INVALID");
            }

            else
            {
                //log response/ipn data for manual investigation
            }
       }
        else
       {

            //  Utility.writeError("IPN example no parameters");
        }
    }
}